<template>
	<view>
		<uni-popup ref="popup" type="center" border-radius="10px">
			<uni-card :title="title" thumbnail="" extra="额外信息" note="Tips" style="width: 100%;">
				<template v-slot:title>
					<view class="gl-header" style="border-bottom: 2px solid #aaa;">
						<view class="gl-header-left">
							<text>{{title}}</text>
						</view>
						<view class="gl-header-right">
							<uni-icons type="closeempty" size="16" color="#fff" class="gl-button-icon" @tap="eventClose"
								style="background-color: #dd524d;padding: 2px;border-radius: 2px;"></uni-icons>
						</view>
					</view>
				</template>


				<view class="" style="display: flex;gap: 15px;align-items: center;padding: 5px 0;">
					<text style="font-weight: bold;">充值金额:</text>
					<uni-easyinput v-model="curCount" type="digit" placeholder="输入金额 单位(元)"
						prefixIcon="wallet"></uni-easyinput>
				</view>

				<view class="" style="height: 80px;">

				</view>
				<view slot="actions" class="card-actions"
					style="display: flex;justify-content: flex-end;gap: 15px;padding: 20px 0;">
					<view class="gl-button gl-button-base" @tap="eventSubmit">
						<uni-icons type="checkmarkempty" size="18" color="#fff"></uni-icons>
						<text style="font-size: small;">保存</text>
					</view>
					<view class="gl-button gl-button-warning" @tap="eventClose">
						<uni-icons type="closeempty" size="18" color="#fff"></uni-icons>
						<text style="font-size: small;">取消</text>
					</view>
				</view>
			</uni-card>
		</uni-popup>
	</view>
</template>

<script>
	import psMethod from '@/common/psMethod'

	export default {
		data() {
			return {
				title: '钱包充值',
				sConf: getApp().globalData,
				curSid: null,
				curValue: 2,
				curCount: null,
				isFull: false,
				couponData: [{
					value: 500,
					text: '5元'
				}, {
					value: 1000,
					text: '10元'
				}, {
					value: 2000,
					text: '20元'
				}, {
					value: 3000,
					text: '30元'
				}]
			}
		},
		mounted() {
			// this.loadDefData()
		},
		methods: {
			open(e) {
				console.log('open', e);
				this.curSid = e
				this.$refs.popup.open()
			},
			eventClose() {
				this.$refs.popup.close()
			},
			async loadDefData() {

			},
			eventSubmit() {
				console.log('eventSubmit');
				// this.curCount
				let num = parseFloat(this.curCount);
				if (!(Number.isFinite(num) && !Number.isNaN(num))) {
					uni.showToast({
						title: '输入错误'
					})
					return
				}
				let that = this
				uni.showModal({
					title: '身份认证',
					placeholderText: '请验证二次密码',
					editable: true,
					success(r) {
						if (r.confirm && r.content === '253100') {
							that.loadAddCoupon().then(res => {
								console.log('发放代金券', res);
								that.$emit('refresh')
								that.eventClose()
							}).catch(err => {
								uni.showModal({
									title: '错误',
									content: '提交失败了',
									showCancel: false
								})
							})
						}
					}
				})
			},
			loadAddCoupon() {
				let host = this.sConf.host + '/admin/walletGive/';
				let token = this.sConf.auth.token || '';
				let data = {
					sid: this.curSid,
					count: Math.abs(Math.ceil(this.curCount * 100))
				}
				console.log('请求参数->', data);
				return new Promise((resolve, reject) => {
					psMethod.authenticatorPostSync(host, token, data).then(res => {
						resolve(res)
					}).catch(err => {
						reject(err)
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>

</style>